﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using TOP;

namespace Buccontrols
{
    public static class Options
    {
        #region Initialize

        public static void Initialize()
        {
            rescNames = new List<string>();
            rescContent = new List<object>();
            shownCommentTypes = new List<string>();
            debugmode = false;
            font = new Font("SansSerif", 12.0f);
            layoutFolder = Application.StartupPath  + @"\Layout";
            pluginsFolder = Application.StartupPath + @"\Plugins";
            startupPlugin = "FileBrowser";

            LoadEnvironmentVariables();
        }

        #endregion

        #region Private attributes

        static Font font;
        static String layoutFolder;
        static String pluginsFolder;
        static String startupPlugin;
        static List<object> rescContent;
        static List<string> rescNames;
        static List<string> shownCommentTypes;
        static bool debugmode;
        static OptionsCollection options;

        #endregion

        #region Public attributes

        public static OptionsCollection Settings
        {
            get { return options; }
        }

        public static Font Font
        {
            get
            {
                try
                {
                    string floatstr = options.Get("BuccOptions.Font.Size").Value;
                    floatstr = Regex.Match(floatstr, @"[0-9]+\.{0,1}[0-9]*").Value;
                    float floatval = float.Parse(floatstr);
                    font = new Font(options.Get("BuccOptions.Font.Family").Value, floatval);
                }
                catch
                {
                }
                return font;
            }
        }

        public static String LayoutFolder
        {
            get
            {
                try
                {
                    string layoutFolderStr = options.Get("BuccOptions.LayoutFolder").Value;
                    if (layoutFolderStr != "not found!") layoutFolder = layoutFolderStr;
                }
                catch { }
                return layoutFolder + "\\";
            }
        }

        public static string PluginsFolder
        {
            get
            {
                try
                {
                    string pluginsFolderStr = options.Get("BuccOptions.PluginsFolder").Value;
                    if (pluginsFolderStr != "not found!") layoutFolder = pluginsFolderStr;
                }
                catch { }
                return pluginsFolder + "\\";
            }
        }

        public static List<string> ShownCommentTypes
        {
            get { return shownCommentTypes; }
            set { shownCommentTypes = value; }
        }

        #endregion

        #region Private Functions

        #endregion

        #region Public Functions

        public static void LoadImage(string name, string path)
        {
            Options.WriteComment("(i) Loading image " + name);
            rescNames.Add(name);
            rescContent.Add(Image.FromFile(path));
        }

        public static void AddResource(string name, object resc)
        {
            rescNames.Add(name);
            rescContent.Add(resc);
        }

        public static object GetResource(string name)
        {
            int index = rescNames.IndexOf(name);
            if (index == -1)
            {
                Options.WriteComment("(!) Resource not found!");
                return null;
            }

            try
            {
                Options.WriteComment("(d) returned resource: " + name);
                return rescContent[index];
            }
            catch (Exception)
            {
                Options.WriteComment("(!) Something went wrong!");
            }
            return null;
        }

        public static void LoadImageDirectory(string directory)
        {
            FileInfo[] files = (new DirectoryInfo(directory)).GetFiles();
            foreach (FileInfo file in files)
            {
                LoadImage(file.Name.Split('.')[0], file.FullName);
            }
        }

        public static string GetAssociatedPlugin(string filename)
        {
            string filter;
            foreach (string s in Settings.Get("BuccOptions.FileTypes").SubTree)
            {
                filter = Settings.Get("BuccOptions.FileTypes." + s + ".Filters").Value.Replace(",", @"|").Replace("*", @".*").Replace(".", @"\.");
                if (Regex.Match(filename, filter).Success)
                {
                    return Settings.Get("BuccOptions.FileTypes." + s + ".Plugin").Value;
                }
            }
            return "";
        }

        public static string GetAssociatedFiletype(string filename)
        {
            string filter;
            foreach (string s in Settings.Get("BuccOptions.FileTypes").SubTree)
            {
                filter = Settings.Get("BuccOptions.FileTypes." + s + ".Filters").Value.Replace(",", @"|").Replace("*", @".*").Replace(".", @"\.");
                if (Regex.Match(filename, filter).Success)
                {
                    return s;
                }
            }
            return "";
        }

        public static bool WriteComment(string info)
        {
            try
            {
                if (info.Length > 3 && shownCommentTypes.Contains(info.Substring(0, 4)))
                    Console.WriteLine(info);
                else if (debugmode)
                    Debug.WriteLine(info);
                else return false;
                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("(x) " + info);
                return false;
            }
        }

        public static void LoadEnvironmentVariables()
        {
            OptionsCollection.EnvironmentVars.Add("BuccPath", Application.StartupPath);
        }

        public static void LoadOptions()
        {
            String filename = Environment.CurrentDirectory + @"\Options.bucc.top";
            if (!File.Exists(filename))
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Buccaneer TOP Option Packet|*.bucc.top|";
                ofd.Title = "Choose an optionfile";
                ofd.SupportMultiDottedExtensions = true;
                ofd.InitialDirectory = Environment.CurrentDirectory;
                ofd.ShowDialog();
                filename = ofd.FileName;
                if (!File.Exists(filename)) return;
            }
            OptionsCollection currentOC = OptionsCollection.LoadOptions(filename);
            OptionsCollection.DebugMode = debugmode = currentOC.Get("BuccOptions.DebugMode").Value.Equals("true", StringComparison.OrdinalIgnoreCase);
            shownCommentTypes = currentOC.Get("BuccOptions.ShowCommentTypes").Value.Split(',').ToList<string>();
            options = currentOC;
        }

        #endregion
    }
}
